<template>
  <div id="user-login-page">
<!--    <div class="title"> 鸡哥待办事项管理系统登录页面</div>-->
    <a-form
      :model="formState"
      class="login-form"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <a-form-item
        :rules="[{ required: true, message: '请输入您的用户名！' }]"
        label="用户名"
        name="username"
      >
        <a-input v-model:value="formState.username">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        :rules="[{ required: true, message: '请输入您的密码！' }]"
        label="密码"
        name="password"
      >
        <a-input-password v-model:value="formState.password">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        :rules="[{ required: true, message: '请输入验证码！' }]"
        label="验证码"
        name="code"
      >
        <div class="captcha-container">
          <a-input v-model:value="formState.code" placeholder="请输入验证码" />
          <img :src="captchaUrl" alt="验证码" class="captcha-image" @click="refreshCaptcha" />
        </div>
      </a-form-item>

      <a-form-item>
        <a-row style="width: 100%">
          <a-col>
            <div class="btn-register">
              <a href="/user/register">没有账号？去注册</a>
            </div>
          </a-col>
          <a-col>
            <div class="btn-reset-password">
              <a href="/user/reset">忘记密码？</a>
            </div>
          </a-col>
        </a-row>
      </a-form-item>

      <a-form-item>

      </a-form-item>

      <a-form-item>
        <a-button :disabled="disabled" class="btn-login" html-type="submit" type="primary">
          登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { LockOutlined, UserOutlined } from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { useRouter } from 'vue-router'
import { getCaptchaUsingGet } from '@/api/captchaController.ts'
import { message } from 'ant-design-vue'
import { userLoginUsingPost } from '@/api/userController.ts'

const router = useRouter()

interface FormState {
  username: string;
  password: string;
  code: string;
}

const formState = reactive<API.UserLoginRequest>({
  username: '',
  password: '',
  code:''
})

const disabled = computed(() => {
  return !(formState.username && formState.password)
})

const loginUserStore = useLoginUserStore()

const onFinish = async (values: any) => {
  const res = await userLoginUsingPost(values)
  //登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    await router.push({
      path: '/my',
      replace: true
    })
  } else {
    message.error('登录失败：' + res.data.message)
  }
}

const captchaUrl = ref('');

const refreshCaptcha = async () => {
  const res = await getCaptchaUsingGet();
  if (res.data.code === 0 && res.data.data) {
    captchaUrl.value = res.data.data;
    formState.code = '';
  }
  else {
    message.error('获取验证码失败：'+res.data.message)
  }
};

onMounted(()=>{
  refreshCaptcha()
})
</script>
<style scoped>
#user-login-page {
  width: 30%;
  margin-left: 35%;
  margin-top: 8%;

}

.btn-login {
  text-align: center;
}

.btn-register {
  text-align: left;
}

.btn-reset-password {
  text-align: right;
}

.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.captcha-image {
  cursor: pointer;
  height: 60px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
</style>
